﻿/*===================================================
* CLR版本  : 4.0.30319.42000 
* 类名称   : MarkFileHelper
* 文 件 名 : MarkFileHelper 
* 类描述   :
* 创建人   : oream
* 创建时间 : 2018/2/18 22:20:03
* 修改人   : 
* 修改时间 :
* 版本： @version 1.0
=====================================================*/

using LitJson;
using UnityEngine;
using System.IO;
using UnityEditor;
using Model.AssetBundle.Entity;
using UnityEditor.SceneManagement;
using UnityEngine.SceneManagement;

namespace Model.AssetBundle.Holper
{
  public static class MarkFileHelper
  {
    private static long totalSize = 0;

    #region 标记全部
    public static void MarkAll()
    {
      EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
      BundleInfoManager.Instance.Dispose();
      totalSize = 0;
      GetPath();
      SaveBundleInfo();
    }

    private static void GetPath()
    {
      AssetDatabase.RemoveUnusedAssetBundleNames();
      string path = Application.dataPath + "/Bundle";

      GetBundleInfo(path);
      BundleInfoManager.Instance.version.TotalSize = totalSize;
      BundleInfoManager.Instance.version.version = "1.0";
      foreach (var item in BundleInfoManager.Instance.version.TotalInfo)
      {
        Debug.Log($"{item.BundleName}>>>{item.BundlePath}>>>{item.BundleSize}>>>{item.MD5}");
      }
    }

    private static void GetBundleInfo(string path)
    {
      DirectoryInfo dirInfo = new DirectoryInfo(path);

      FileSystemInfo[] fileInfos = dirInfo.GetFileSystemInfos();


      foreach (var item in fileInfos)
      {
        if (!(item is DirectoryInfo))
        {
          FileInfo fileInfo = item as FileInfo;
          if (fileInfo.Extension == ".meta") continue;
          SetTag(fileInfo);
          continue;
        }
        GetBundleInfo(item.FullName);
      }
    }

    private static void SetTag(FileInfo path)
    {
      BundleInfoEntity bundleInfoEntity = new BundleInfoEntity();
      int tmpCutSite = path.FullName.IndexOf("Bundle");
      string shortPath = path.FullName.Substring(tmpCutSite, path.FullName.Length - tmpCutSite).Replace('\\', '/');
      string[] tmp = shortPath.Substring(shortPath.IndexOf("/") + 1).ToLower().Split('/');
      AssetImporter assetImporter = AssetImporter.GetAtPath("Assets/" + shortPath);
      if (tmp.Length > 2)
      {
        assetImporter.assetBundleName = tmp[0] + "/" + tmp[1];

      }
      else
      {
        assetImporter.assetBundleName = tmp[0];
      }
      SetSuffix(path, assetImporter);
      bundleInfoEntity.BundlePath = shortPath;
      bundleInfoEntity.BundleName = assetImporter.assetBundleName + '.' + assetImporter.assetBundleVariant;
      bundleInfoEntity.BundleSize = path.Length;
      bundleInfoEntity.MD5 = MD5Holper.FileMD5(path.FullName);
      BundleInfoManager.Instance.version.TotalInfo.Add(bundleInfoEntity);
      totalSize += path.Length;
    }

    private static void SetSuffix(FileSystemInfo path, AssetImporter assetImporter)
    {
      if (path == null) return;
      FileInfo fileInfo = path as FileInfo;
      if (fileInfo.Extension == ".unity")
      {
        assetImporter.assetBundleVariant = "u3d";
      }
      else
      {
        assetImporter.assetBundleVariant = "ld";
      }
    }

    private static void SaveBundleInfo()
    {
      string json = JsonMapper.ToJson(BundleInfoManager.Instance.version);
      Debug.Log(json);
     
    }
    #endregion

    #region 清除所有标记
    public static void RemoveAllMark()
    {
      foreach (var item in AssetDatabase.GetAllAssetBundleNames())
      {
        AssetDatabase.RemoveAssetBundleName(item, true);
      }
      BundleInfoManager.Instance.Dispose();
    }
    #endregion
  }
}
